#!/usr/bin/python
#-----------------------------------------------------------------------------
# 
#
# Usage: 
# 
#-----------------------------------------------------------------------------
# Copyright 2007, Oliver White
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------
from base import pyrouteModule
import os
import cairo

class tracklog_overlay(pyrouteModule):
  """  """
  def __init__(self, modules):
    pyrouteModule.__init__(self, modules)
    self.debug = False
    self.pgpx = None
    self.ss = 0
    self.sp = 0
    self.es = -1
    self.ep = -1
    if self.debug: print "tracklog_overlay __init__ - modules=%s." % modules

  def setGpx(self,pgpx):
    self.pgpx = pgpx
    self.ss = 0
    self.sp = 0
    self.es = -1
    self.ep = -1

  def setDrawRange(self,ss,sp,es,ep):
    self.ss = ss
    self.sp = sp
    self.es = es
    self.ep = ep

  def draw(self,cr,proj):
    """Draw the tracklog to the projection, between start segment ss, point sp,
    and end segment es, point ep"""
    if self.debug: print "tracklog_overlay.draw()"

    if self.pgpx==None: return

    s_seg = self.ss
    s_pt = self.sp
    e_seg = self.es
    e_pt = self.ep
    cr.set_line_cap(cairo.LINE_CAP_ROUND)
    cr.set_source_rgb(0.7,0.7,0)
    cr.set_line_width(5)

    if self.debug: 
      print "getTrackAnalysis: s_seg=%d, s_pt=%d, e_seg=%d, e_pt=%d\n" % \
          (s_seg,s_pt,e_seg,e_pt)
    if not self.pgpx.segNoValid(s_seg):
      print "segment numbers must lie in the range 0 to %d.\n" % (self.getNumTrkSeg()-1)
      return -1
    if not self.pgpx.segNoValid(e_seg):
      print "segment numbers must lie in the range 0 to %d.\n" % (self.getNumTrkSeg()-1)
      return -1
      
    if not self.pgpx.ptNoValid(s_seg,s_pt):
      print "Start point out of range - must be in range %d to %d\n" % \
          (-1,self.getNumPts(s_seg)-1)
      return -1
    if e_pt == -1:
      e_pt = self.pgpx.getNumPts(e_seg)-1
    if not self.pgpx.ptNoValid(e_seg,e_pt):
      print "Start point out of range - must be in range %d to %d\n" % \
          (-1,self.pgpx.getNumPts(e_seg)-1)
      return -1

    if e_seg==-1:
        e_seg = self.pgpx.getNumTrkSeg()-1
        if self.debug: print "Default e_seg used - set to %d" % e_seg
    else:
        if self.debug: print "e_seg=%s" % e_seg


    if e_pt==-1:
        e_pt = self.pgpx.getNumPts(e_seg)-1
        if self.debug: print "Default e_pt used - set to %d" \
                % e_pt
    else:
        if self.debug: print "e_pt=%d" % e_pt



    for seg in range(s_seg,e_seg+1):
      # Start and end points for current segment
      seg_s_pt = -1
      seg_e_pt = -1
      if (seg==s_seg):
        seg_s_pt = s_pt
      else:
        seg_s_pt = 0

      if (seg==e_seg):
        seg_e_pt = e_pt
      else:
        seg_e_pt = self.pgpx.getNumPts(seg)-1


      if self.debug:
        print "draw(): Drawing segment %d, from point %d to point %d." % (seg,seg_s_pt,seg_e_pt)

      first = 1
      for pt in range(seg_s_pt,seg_e_pt+1):
        trkpt = self.pgpx.getTrkPt(seg,pt)
        #print "seg=%d, pt=%d trkpt=%s" % (seg,pt,trkpt)
        # Check max and min time
        x,y = proj.ll2xy(trkpt['lat'], trkpt['lon'])
        if (first):
          cr.move_to(x,y)
          first = 0
        else:
          cr.line_to(x,y)
      cr.stroke()

